//
//  WeiboViewController.m
//  Objc进阶Demo
//
//  Created by Don't update on 2018/7/18.
//  Copyright © 2018年 Don't update. All rights reserved.
//

#import "WeiboViewController.h"
#define screenW [UIScreen mainScreen].bounds.size.width
#define screenH [UIScreen mainScreen].bounds.size.height
#define headViewH  150
#define naviBarH self.navigationController.navigationBar.frame.size.height
@interface WeiboViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)UIView *headView;
@property(nonatomic,strong)UISegmentedControl *segment;
@property(nonatomic,strong)UISegmentedControl *segment1;
@property(nonatomic,strong)NSArray *list;

@end

@implementation WeiboViewController


-(NSArray *)list{
    if (_list == nil) {
        NSMutableArray *arr = [[NSMutableArray alloc]init];
        for (int i = 0; i<50; i++) {
            [arr addObject:[NSString stringWithFormat:@"第 %d 个cell",i]];
        }
        _list = [NSArray arrayWithArray:arr];
    }
    return _list;
}



-(UIView *)headView{
    if (_headView == nil) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, -50, screenW, headViewH+30)];
        _headView.backgroundColor = [UIColor blueColor];
        [self segment];
    }
    return  _headView;
}

-(UISegmentedControl *)segment{
    if (_segment == nil) {
        NSArray *titles = [[NSArray alloc]initWithObjects:@"one",@"two",@"three", nil];
        _segment = [[UISegmentedControl alloc]initWithItems:titles];
        _segment.frame = CGRectMake(0, headViewH, screenW, 30);
        [self.headView addSubview:_segment];
    }
    return _segment;
}

-(UISegmentedControl *)segment1{
    if (_segment == nil) {
        NSArray *titles = [[NSArray alloc]initWithObjects:@"one",@"two",@"three", nil];
        _segment1 = [[UISegmentedControl alloc]initWithItems:titles];
        _segment1.backgroundColor = [UIColor blackColor];
        _segment1.frame = CGRectMake(0,64, screenW, 30);
        [self.headView addSubview:_segment1];
    }
    return _segment1;
}


-(UITableView *)tableview{
    if (_tableview == nil) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, -30, screenW,screenH) style:UITableViewStyleGrouped];
        [self headView];
        _tableview.tableHeaderView = _headView;
        _tableview.scrollEnabled = YES;
        _tableview.delegate = self;
        _tableview.dataSource = self;
        [self list];
        [self.view addSubview:_tableview];
    }
    return  _tableview;
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _list.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifity = @"identifity";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifity];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:identifity];
    }
    cell.textLabel.text = _list[indexPath.row];
    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y>63) {
        [_segment1 setHidden:NO];
        [self.navigationController.navigationBar setHidden:NO];
    }
    else{
        [_segment1 setHidden:YES];
        [self.navigationController.navigationBar setHidden:YES];
    }
}




- (void)viewDidLoad {
    [super viewDidLoad];
    [self segment1];
    [self tableview];
    [self.view addSubview:_segment1];
    [self.segment1 setHidden:YES];
    [self.navigationController.navigationBar setHidden:YES];

}



@end
